<?php

namespace emqx;

use app\admin\model\iot\Device;
use think\response\Json;

class Emqx
{
    //EmqxMqtt服务器端
    public string $emqx_mqtt_host;
    public int $emqx_mqtt_port;
    public string $emqx_mqtt_php_username;
    public string $emqx_mqtt_php_password;
    //设备端密码
    public string $emqx_mqtt_device_topic;
    public string $emqx_mqtt_device_password;
    //EmqxHttpApi
    public string $emqx_http_host;
    public string $emqx_appid;
    public string $emqx_secret;
    //数据大屏websocket
    public string $emqx_websocket_host;
    public string $emqx_datacenter_topic_js;
    public string $emqx_datacenter_username;
    public string $emqx_datacenter_password;
    //客户端扫码取袋订阅的主题
    public string $emqx_scan_bag_host;
    public string $emqx_scan_bag_topic_js;
    public string $emqx_scan_bag_username;
    public string $emqx_scan_bag_password;
    public string $emqx_wss_websocket_host;

    /**
     * 初始化
     */
    public function __construct()
    {
        //根据不同环境获取配置信息
        switch (env('ENVIRONMENT')) {
            case 'dev':
                //EmqxMqtt客户端
                $this->emqx_mqtt_host = '106.55.164.145';
                $this->emqx_mqtt_port = 1883;
                $this->emqx_mqtt_php_username = 'iot_php_master_2021';
                $this->emqx_mqtt_php_password = 'd7ff96f54afc977ec3207c55a431e5fd1821edc822f939170c78663b93c81967';
                //EmqxHttpApi
                $this->emqx_http_host = $this->emqx_mqtt_host . ':8081';
                $this->emqx_appid = '2c0ab2229e5ce';
                $this->emqx_secret = 'MzAyNzk1NDQ2NjUzOTA3MTQ0OTg3NDY1MjMzNTg5ODYyNDA';
                break;
            default:
                //EmqxMqtt客户端
                $this->emqx_mqtt_host = '120.24.206.44';
                $this->emqx_mqtt_port = 1883;
                $this->emqx_mqtt_php_username = 'iot_php_master_2021';
                $this->emqx_mqtt_php_password = 'd7ff96f54afc977ec3207c55a431e5fd1821edc822f939170c78663b93c81967';
                //EmqxHttpApi
                $this->emqx_http_host = $this->emqx_mqtt_host . ':8081';
                $this->emqx_appid = '2c0ab2229e5ce';
                $this->emqx_secret = 'MzAyODU3NTU5MTkwOTcwOTgzMzgwMDA0NDkzMjc5MjMyMDA';
                break;
        }
        //设备端密码
        $this->emqx_mqtt_device_topic = '$DEVICE/iot/php/2021';
        $this->emqx_mqtt_device_password = 'b0ebcbcbf631d0e2980677529d323457c86c8d2f46c241ac78da60ac570e1585';
        //websocket
        $this->emqx_websocket_host = 'ws://' . $this->emqx_mqtt_host . ':8083/mqtt';
//        $this->emqx_wss_websocket_host = 'wss://' . $this->emqx_mqtt_host . ':8084/mqtt';
        $this->emqx_wss_websocket_host = 'wss://' .str_replace('https://', '', env('ApiDomainV2')). '/mqtt';
        //数据大屏订阅的主题
        $this->emqx_datacenter_topic_js = '$Datacenter/iot/js/2022';
        $this->emqx_datacenter_username = 'datacenter_iot_js_2022';
        $this->emqx_datacenter_password = 'd7ff96f54afc977ec3207c55a431e5fd1821edc822f939170c78663b93c81967';
        //客户端扫码取袋订阅的主题
        $this->emqx_scan_bag_host = str_replace('https://', '', env('ApiDomainV2')) . '/mqtt';
        $this->emqx_scan_bag_topic_js = '$Scanbag/iot/js/orderNo/';
        $this->emqx_scan_bag_username = 'scan_bag_iot_js_2022';
        $this->emqx_scan_bag_password = 'd7ff96f54afc977ec3207c55a431e5fd1821edc822f939170c78663b93c81967';
    }

    /**
     * EMQX认证回调
     * @param array $post_data
     * @return Json
     */
    public function emqxMqttAuth(array $post_data): Json
    {
        //根据用户判断
        switch ($post_data['username']) {
            case $this->emqx_mqtt_php_username:
                //PHP用户为最高权限
                break;
            case $this->emqx_datacenter_username:
                //数据大屏订阅的主题
                if ($post_data['password'] != $this->emqx_datacenter_password) {
                    return json([], 500);
                }
                break;
            case $this->emqx_scan_bag_username:
                //客户端扫码取袋订阅的主题
                if ($post_data['password'] != $this->emqx_scan_bag_password) {
                    return json([], 500);
                }
                break;
            default:
                //如果账号或密码为空则返回验证失败
                if (!$post_data['username'] or !$post_data['password']) {
                    return json([], 500);
                }
                //客户端设备通信
                $len = strlen($post_data['username']);
                if (!in_array($len, [15, 20]) or $post_data['password'] != $this->emqx_mqtt_device_password) {
                    return json([], 500);
                }
                //判断当前设备是否存在
                $where = ['imei' => $post_data['username']];
                $imei = Device::where($where)->cache(true, 600)->value('imei');
                if (!$imei) {
                    return json([], 500);
                }
                break;
        }
        return json([]);
    }

    /**
     * EMQX权限回调
     * @param array $post_data
     * @return Json
     */
    public function emqxMqttAcl(array $post_data): Json
    {
        //根据用户判断
        switch ($post_data['username']) {
            case $this->emqx_mqtt_php_username:
                //PHP用户为最高权限
                break;
            case $this->emqx_datacenter_username:
                //数据大屏订阅的主题(限定只能订阅这一个主题,不能发布消息)
                if ($post_data['access'] != '1' or $post_data['topic'] != $this->emqx_datacenter_topic_js) {
                    return json([], 500);
                }
                break;
            case $this->emqx_scan_bag_username:
                break;
                //客户端扫码取袋订阅的主题(限定只能订阅这一个主题,不能发布消息)
                if ($post_data['access'] != '1') {
                    return json([], 500);
                }
                //字符串截取
                $post_t = substr($post_data['topic'], 0, strlen($this->emqx_scan_bag_topic_js));
                if ($post_t != $this->emqx_scan_bag_topic_js) {
                    return json([], 500);
                }
                break;
            default:
                //客户端设备通信
                $len = strlen($post_data['username']);
                if (!in_array($len, [15, 20])) {
                    return json([], 500);
                }
                //根据订阅和发布进行权限控制
                switch ($post_data['access']) {
                    case '1'://订阅
                        if ($post_data['username'] != $post_data['topic']) {
                            return json([], 500);
                        }
                        break;
                    case '2'://发布
                        if ($post_data['topic'] != $this->emqx_mqtt_device_topic) {
                            return json([], 500);
                        }
                        break;
                    default:
                        return json([], 500);
                }
                //判断当前设备是否存在
                $where = ['imei' => $post_data['username']];
                $imei = Device::where($where)->cache(true, 600)->value('imei');
                if (!$imei) {
                    return json([], 500);
                }
                break;
        }
        return json([]);
    }

}
